﻿//using System;
//using System.Collections.Generic;
//using System.IO;
//using System.Linq;
//using System.Net;
//using System.Text;
//using System.Text.RegularExpressions;
//using System.Web;
//using System.Web.Script.Serialization;

//namespace WebChatServer.Models
//{
//    public class YahooHelper
//    {
//        const string URL_OAUTH_DIRECT = "https://login.yahoo.com/WSLogin/V1/get_auth_token";
//        const string URL_OAUTH_ACCESS_TOKEN = "https://api.login.yahoo.com/oauth/v2/get_token";
//        const string URL_YM_SESSION = "http://developer.messenger.yahooapis.com/v1/session";
//        const string URL_YM_PRESENCE = "http://developer.messenger.yahooapis.com/v1/presence";
//        const string URL_YM_CONTACT = "http://developer.messenger.yahooapis.com/v1/contacts";
//        const string URL_YM_MESSAGE = "http://developer.messenger.yahooapis.com/v1/message/yahoo/{{USER}}";
//        const string URL_YM_NOTIFICATION = "http://developer.messenger.yahooapis.com/v1/notifications";
//        const string URL_YM_NOTIFICATION_LONG = "http://{{NOTIFICATION_SERVER}}/v1/pushchannel/{{USER}}";
//        const string URL_YM_BUDDYREQUEST = "http://developer.messenger.yahooapis.com/v1/buddyrequest/yahoo/{{USER}}";
//        const string URL_YM_GROUP = "http://developer.messenger.yahooapis.com/v1/group/{{GROUP}}/contact/yahoo/{{USER}}";

//        protected string Oauth;
//        protected Dictionary<string, string> Token;
//        protected Dictionary<string, string> Ym;
//        protected Dictionary<string, string> AccessToken;

//        //protected Config Config;
//        protected string Error;

//        protected Dictionary<string, string> Config = new Dictionary<string, string>();

//        public bool Includeheader = false;	
//        public bool Debug = false;

//        /*
//         * constructor
//         */
//        public void Construct(string consumerkey, string secretkey, string username, string password)
//        {
//            Config.Add("consumer_key", consumerkey);
//            Config.Add("secret_key", secretkey);
//            Config.Add("username", username);
//            Config.Add("password", password);

//            // Gau lam :">
//            Ym = new Dictionary<string, string>();
//            Error = null;
//        }

//        // Gau lam :">
//        public bool FetchRequestToken()
//        {
//            string url = YahooHelper.URL_OAUTH_DIRECT
//                         + "?login=" + Config["username"]
//                         + "&passwd=" + Config["password"]
//                         + "&oauth_consumer_key=" + Config["consumer_key"];

//            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//            string rs = GetReponseContent(request, "GET");

//            if (rs.Contains("RequestToken") == false)
//                return false;

//            string requestToken = rs.Replace("RequestToken=", "").Trim();
//            this.Token.Add("request", requestToken);
//            return true;
//        }

//        // Heo lam thu ik :x
//        public bool FetchAccessToken()
//        {
//            string url = YahooHelper.URL_OAUTH_ACCESS_TOKEN
//                         + "?oauth_consumer_key=" + Config["consumer_key"]
//                         + "&oauth_nonce=" + new Guid((new Random()).ToString())
//                         + "&oauth_signature=" + Config["secret_key"] + "%26"
//                         + "&oauth_signature_method=PLAINTEXT"
//                         + "&oauth_timestamp=" + GetTimeStamp()
//                         + "&oauth_token=" + Token["request"]
//                         + "&oauth_version=1.0";
//            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
//            string rs = GetReponseContent(request, "GET");

//            if(rs.Contains("oauth_token") == false)
//            {
//                Error = rs;
//                return false;
//            }

//            string[] tmp = Regex.Split(rs, "&");
//            AccessToken = new Dictionary<string, string>();
            
//            for(int i = 0; i < tmp.Length; i++)
//            {
//                string[] col = Regex.Split(tmp[i], "=");             
//                AccessToken[col[0]] = col[1];
//            }
//            Token["access"] = (new JavaScriptSerializer()).Serialize(AccessToken);
//            return true;
//        }

//        public bool FetchCrumb()
//        {
//            string url = YahooHelper.URL_YM_SESSION
//                         + "?oauth_consumer_key=" + Config["consumer_key"]
//                         + "&oauth_nonce=" + new Guid((new Random()).ToString())
//                         + "&oauth_signature=" + Config["secret_key"] + "%26" +
//                         (new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(Token["access"]))[
//                             "oauth_token_secret"]
//                         + "&oauth_signature_method=PLAINTEXT"
//                         + "&oauth_timestamp=" + GetTimeStamp()
//                         + "&oauth_token=" +
//                         (new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(Token["access"]))[
//                             "oauth_token"]
//                         + "&oauth_version=1.0";


//            string header = "Content-type: application/json; charset=utf-8";

//            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//            request.ContentType = header;
//            string rs = GetReponseContent(request, "GET");

//            if (rs.Contains("crumb") == false)
//                return false;

//            string js = new JavaScriptSerializer().Deserialize<string>(rs);
//            Ym["crumb"] = js;

//            return true;
//        }

//        public bool SignOn(string status, int state)
//        {
//            string url = YahooHelper.URL_YM_SESSION
//                         + "?oauth_consumer_key=" + Config["consumer_key"]
//                         + "&oauth_nonce=" + new Guid((new Random()).ToString())
//                         + "&oauth_signature=" + Config["secret_key"] + "%26" +
//                         (new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(Token["access"]))[
//                             "oauth_token_secret"]
//                         + "&oauth_signature_method=PLAINTEXT"
//                         + "&oauth_timestamp=" + GetTimeStamp()
//                         + "&oauth_token=" +
//                         (new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(Token["access"]))[
//                             "oauth_token"]
//                         + "&oauth_version=1.0"
//                         + "&notifyServerToken=1";

//            string header = "Content-type: application/json; charset=utf-8";
//            string postdata = "{\"presenceState\" : " + state + ", \"presenceMessage\" : \"" + status + "\"}";
//            //byte[] byteArray = Encoding.UTF8.GetBytes(postdata);

//            Includeheader = true;

//            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//            request.ContentType = header;
//            request.ContentLength = postdata.Length;
//            StreamWriter stream = new StreamWriter(request.GetRequestStream());
//            stream.Write(postdata);
//            stream.Close();

//            string rs = GetReponseContent(request, "POST");

//            string body = null;
//            string[] list = rs.Split(new char[]{'\r', '\n', '\r', '\n'}, 2);
//            header = list[0];
//            body = list[1];

//            string notifytoken = "";
//            Match m = null;

//            if ((m = Regex.Match(header, "/set-cookie: IM=(.+?); expires/")) != null)
//                notifytoken = m.Groups[1].Value;
//            if (body.Contains("sessionId") == false)
//                return false;

//            Dictionary<string, string> js = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(body);
//            js["notifytoken"] = notifytoken;
//            Ym["signon"] = new JavaScriptSerializer().Serialize(js);

//            return true;
//        }

//        public bool SignOff()
//        {
//            string url = YahooHelper.URL_YM_SESSION
//                         + "?oauth_consumer_key=" + Config["consumer_key"]
//                         + "&oauth_nonce=" + new Guid((new Random()).ToString())
//                         + "&oauth_signature=" + Config["secret_key"] + "%26" + AccessToken["oauth_token_secret"]
//                         + "&oauth_signature_method=PLAINTEXT"
//                         + "&oauth_timestamp=" + GetTimeStamp()
//                         + "&oauth_token=" + AccessToken["oauth_token"]
//                         + "&oauth_version=1.0"
//                         + "&sid=" +
//                         (new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(Ym["signon"]))["sessionId"];

//            string header = "Content-type: application/json; charset=utf-8";

//            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//            request.ContentType = header;
//            string rs = GetReponseContent(request, "DELETE");
//            return true;
//        }

//        public bool ChangePresence(string status, int state)
//        {
//            string url = YahooHelper.URL_YM_PRESENCE
//                        + "?oauth_consumer_key=" + Config["consumer_key"]
//                        + "&oauth_nonce=" + new Guid((new Random().ToString()))
//                        + "&oauth_signature=" + Config["secret_key"] + "%26" + AccessToken["oauth_token_secret"]
//                        + "&oauth_signature_method=PLAINTEXT"
//                        + "&oauth_timestamp=" + GetTimeStamp()
//                        + "&oauth_token=" + AccessToken["oauth_token"]
//                        + "&oauth_version=1.0"
//                        + "&sid=" +
//                        (new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(Ym["signon"]))["sessionId"];

//            string header = "Content-type: application/json; charset=utf-8";
//            string postdata = "{\"presenceState\" : " + state + ", \"presenceMessage\" : \"" + status + "\"}";

//            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//            request.ContentType = header;
//            request.ContentLength = postdata.Length;

//            StreamWriter stream = new StreamWriter(request.GetRequestStream());
//            stream.Write(postdata);
//            stream.Close();

//            string rs = GetReponseContent(request, "PUT");

//            return true;
//        }

//        public bool SendMessage(string user, string message)
//        {
//            string url = YahooHelper.URL_YM_MESSAGE
//                        + "?oauth_consumer_key=" + Config["consumer_key"]
//                        + "&oauth_nonce=" + new Guid((new Random().ToString()))
//                        + "&oauth_signature=" + Config["secret_key"] + "%26" + AccessToken["oauth_token_secret"]
//                        + "&oauth_signature_method=PLAINTEXT"
//                        + "&oauth_timestamp=" + GetTimeStamp()
//                        + "&oauth_token=" + AccessToken["oauth_token"]
//                        + "&oauth_version=1.0"
//                        + "&sid=" +
//                        (new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(Ym["signon"]))["sessionId"];
//            url = Regex.Replace(url, "{{USER}}", user);
            
//            string header = "Content-type: application/json; charset=utf-8";
//            string postdata = "{\"message\" : " +  message + "}";

//            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//            request.ContentType = header;
//            request.ContentLength = postdata.Length;

//            StreamWriter stream = new StreamWriter(request.GetRequestStream());
//            stream.Write(postdata);
//            stream.Close();

//            string rs = GetReponseContent(request, "POST");

//            return true;
//        }

//        public Dictionary<string, string> FetchContactList()
//        {
            
//        } 

//        private static long GetTimeStamp()
//        {
//            var distance = DateTime.Now - new DateTime(1970, 1, 1);
//            var timestamp = (long)distance.TotalSeconds;
//            return timestamp;
//        }

//        // Cai ni tu che, cho no' ngan' :">
//        private string GetReponseContent( HttpWebRequest request, string method)
//        {
//            request.Method = method;
//            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//            return new StreamReader(response.GetResponseStream()).ReadToEnd();
//        }
//    }
//}